<?php
	require_once 'Connect.class.php';
	require_once 'HocVi.class.php';
	require_once 'ChuyenNganh.class.php';
	require_once 'TinhTrangGV.class.php';
	require_once 'Person.class.php';
/**
* 
*/
	class GiangVien extends Person
	{
	

		private $hocVi;

		private $chuyenNganh;
		
		private $tinhTrangGV;

		private $namCT;

		private $heSoPC;

		private $dsChucVu; // array chua ds chuc vu ( kieu chuc vu)


   public function setNamCongTac($namCT){
   		$this->namCT=$namCT;
   }

   public function getNamCongTac(){
   		return $this->namCT;
   }

   public function setHeSoPhanCong($t){
   	$this->heSoPC=$t;
   }

   public function getHeSoPhanCong(){
   	return $this->heSoPC;
   }
	
    public function getDSChucVu(){
    	return $this->dsChucVu;
    }

    public function setDSChucVu($dscv){
    	$this->dsChucVu=$dscv;
    }

	public function setChuyenNganh(ChuyenNganh $cn){
		$this->chuyenNganh=$cn;
	}

	public function getChuyenNganh(){
		return $this->chuyenNganh;
	}
	public function setChucVu(ChucVu $cv){
		$this->chucVu=$cv;
	}

	public function getChucVu(){
		return $this->chucVu;
	}

	public function setHocVi(HocVi $hv){
		$this->hocVi=$hv;
	}
	public function getHocVi(){
		return $this->hocVi;
	}
	public function setTinhTrangGV(TinhTrangGV $ttgv)
	{
		$this->tinhTrangGV=$ttgv;
	}
	public function getTinhTrangGV()
	{
		return $this->tinhTrangGV;
	}
	public function __construct(){
		//-------
	}
	public function getInfo($maGV){

			$conn= new Connect();

			$conn->Query("SELECT * FROM tbl_giang_vien WHERE maGV='".$maGV."'; ");	

			while ($row=$conn->Fetch()) {	

				$this->ma=$row['maGV'];
				
				$this->ten=$row['tenGV'];
				
				$this->hinhAnh_url=$row['hinhAnh'];
				
				$this->tel=$row['tel'];
				
				$this->email=$row['email'];
				
				$this->ngaySinh=$row['ngaySinh'];
				
				$this->matKhau=$row['matKhau'];
				
				$this->namCT=$row['namCT'];
				
				$this->heSoPC=$row['heSoPhanCong'];

				$this->mobile=$row['mobile'];

				$this->cmnd=$row['cmnd'];

				$gt= new GioiTinh();
				$gt->getInfo($row['maGT_FK']);
				$this->gioiTinh=$gt;
				
				$hv= new HocVi();
				
				$hv->getInfo($row['maHV_FK']);
				
				$this->hocVi=$hv;
				
				$cn= new ChuyenNganh();
				
				$cn->getInfo($row['maCN_FK']);
				
				$this->chuyenNganh=$cn;
				
				$tt= new TinhTrangGV();
				
				$tt->getInfo($row['maTTGV_FK']);
				
				$this->tinhTrangGV=$tt;

			}
			// ----------------
			$conn->Query("select maGV_FK,maCV_FK from tbl_giangvien_chucvu a,tbl_giang_vien b, tbl_chuc_vu c where b.maGV=a.maGV_FK and c.maCV=a.maCV_FK and a.maGV_FK='".$this->ma."';");

			$index=0;	

			while ($row=$conn->Fetch()) {	

				$cv=new ChucVu();

				$cv->getInfo($row['maCV_FK']);

				$this->dsChucVu[$index]=$cv;

				$index++;

			}
			// dong ket noi
			$conn->close();
		}

	
	public function them(){

			$sql="INSERT INTO tbl_giang_vien(maGV,tenGV,ngaySinh,matKhau,tel,email,heSoPhanCong,hinhAnh,mobile,cmnd,maGT_FK,namCT,maCN_FK,maHV_FK,maTTGV_FK)";
			
			$sql.=" VALUES('".$this->ma."','".$this->ten."','".$this->ngaySinh."','".$this->matKhau."','".$this->tel."','".$this->email."',";
			
			$sql.="".$this->heSoPC.",'".$this->hinhAnh."','".$this->mobile."','".$this->cmnd."','".$this->gioiTinh->getMa()."',".$this->namCT.",'".$this->chuyenNganh->getMa()."',";
			
			$sql.="'".$this->hocVi->getMa()."','".$this->tinhTrangGV->getMa()."')";
			
			$conn=new Connect();

			$conn->Query($sql);


		foreach ($this->dsChucVu as $key){

			$conn->Query(" INSERT INTO tbl_giangvien_chucvu(maGV_FK,maCV_FK) VALUES('".$this->ma."','".$key->getMa()."' )");
			}
			

			$conn->close();
			
	}


	public function capnhat(){
			$sql="UPDATE tbl_giang_vien SET tenGV='".$this->ten."',ngaySinh='".$this->ngaySinh."',email='".$this->email."',tel='".$this->tel."',namCT=".$this->namCT.",maCN_FK='".$this->chuyenNganh->getMa()."',maHV_FK='".$this->hocVi->getMa()."',";
			
			$sql.=" maTTGV_FK='".$this->tinhTrangGV->getMa()."',hinhAnh='".$this->hinhAnh_url."',mobile='".$this->mobile."',cmnd='".$this->cmnd."',maGT_FK='".$this->gioiTinh->getMa()."',heSoPhanCong=".$this->heSoPC." WHERE maGV='".$this->ma."'";

			$conn=new Connect();
			
			$conn->Query($sql);


			// xoa ds chuc vu 

			$conn->Query("DELETE FROM tbl_giangvien_chucvu WHERE maGV_FK='".$this->ma."'");

			// them moi
			
			foreach ($this->dsChucVu as $key){

				$conn->Query(" INSERT INTO tbl_giangvien_chucvu(maGV_FK,maCV_FK) VALUES('".$this->ma."','".$key->getMa()."' )");
			}
		

			$conn->close();
		}

		
	public function xoa(){
		$sql="DELETE FROM tbl_giang_vien WHERE maGV='".$this->ma."';";
		$conn=new Connect();
		$conn->Query($sql);
		$conn->close();

		}
	public function ds(){
		$ds=array();
		$conn= new Connect();
		$sql="SELECT * FROM tbl_giang_vien";
		$conn->Query($sql);
		$index=0;
		while($row=$conn->Fetch()) {
			$gv= new GiangVien();
			$gv->setMa($row['maGV']);
			$gv->setTen($row['tenGV']);
			$gv->setHinhAnh($row['hinhAnh']);
			$gv->setTel($row['tel']);
			$gv->setEmail($row['email']);
			$gv->setNgaySinh($row['ngaySinh']);
			$gv->setMatKhau($row['matKhau']);
			$gv->setNamCongTac($row['namCT']);
			$gv->setHeSoPhanCong($row['heSoPhanCong']);	
			$gt=new GioiTinh();
			$gt->getInfo($row['maGT_FK']);
			$gv->setGioiTinh($gt);		
			$hv= new HocVi();
			$hv->getInfo($row['maHV_FK']);
			$gv->setHocVi($hv);			
			$cn= new ChuyenNganh();
			$cn->getInfo($row['maCN_FK']);
			$gv->setChuyenNganh($cn);			
			$tt= new TinhTrangGV();
			$tt->getInfo($row['maTTGV_FK']);
			$gv->setTinhTrangGV($tt);			
			$ds[$index]=$gv;
			$index++;
		}

		$conn->close();
		return $ds;
	}

	public function getString($maCN="all",$maTT="all"){

		$sql="SELECT * FROM tbl_giang_vien a,tbl_chuyen_nganh b,tbl_tinhtrang_giangvien c WHERE a.maCN_FK=b.maCN and a.maTTGV_FK=c.maTTGV";

		if ($maCN=="all"){

			if ($maTT=="all"){
				
			} else {
				$sql.=" and c.maTTGV='".$maTT."' ";
			}

		} else {

			if ($maTT=="all"){
				$sql.=" and b.maCN='".$maCN."' ";
			} else {
				$sql.=" and c.maTTGV='".$maTT."' and b.maCN='".$maCN."' ";
			}
		}
		
		$sql.=" order by a.maGV asc ";

		return $sql;

	}

	public function getSortBy($maCN="all",$maTT="all",$page=1){

		$ds=array();

		$conn= new Connect();

		$sql="SELECT maGV,tenGV,hinhAnh,ngaySinh FROM tbl_giang_vien a,tbl_chuyen_nganh b,tbl_tinhtrang_giangvien c WHERE a.maCN_FK=b.maCN and a.maTTGV_FK=c.maTTGV";

		if ($maCN=="all"){

			if ($maTT=="all"){
				
			} else {
				$sql.=" and c.maTTGV='".$maTT."' ";
			}

		} else {

			if ($maTT=="all"){
				$sql.=" and b.maCN='".$maCN."' ";
			} else {
				$sql.=" and c.maTTGV='".$maTT."' and b.maCN='".$maCN."' ";
			}
		}
		
		$sql.=" order by a.maGV asc ";

		$offset=10*($page-1);

		$sql.= " limit ".$offset.",10 ;";
		
		$conn->Query($sql);
		$index=0;
		//echo $conn->getRS()->num_rows;
		while ($row=$conn->Fetch())
		{
			$gv= new GiangVien();
			$gv->setMa($row['maGV']);
			$gv->setTen($row['tenGV']);
			$gv->setHinhAnh($row['hinhAnh']);
			$gv->setNgaySinh($row['ngaySinh']);
			/*
			$gv->setTel($row['tel']);
			$gv->setEmail($row['email']);		
			$gv->setMatKhau($row['matKhau']);
			$gv->setNamCongTac($row['namCT']);
			$gv->setHeSoPhanCong($row['heSoPhanCong']);	
			$gt=new GioiTinh();
			$gt->getInfo($row['maGT_FK']);			
			$gv->setGioiTinh($gt);				
			$hv= new HocVi();
			$hv->getInfo($row['maHV_FK']);
			$gv->setHocVi($hv);			
			$cn= new ChuyenNganh();
			$cn->getInfo($row['maCN_FK']);
			$gv->setChuyenNganh($cn);			
			$tt= new TinhTrangGV();
			$tt->getInfo($row['maTTGV_FK']);
			$gv->setTinhTrangGV($tt);	
			*/			
			$ds[$index]=$gv;
			$index++;
		
		}

		$conn->close();
		return $ds;
	}

	public function exportToExcel(){
		
	}


}
?>